package ui.activity.mine.presenter;


import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import Bean.AddressParamBean;
import Bean.FindAddressBean;
import Bean.GetBankcardParamBean;
import Bean.SetDefaultAddressBean;
import Bean.SetDefaultAddressParamBean;
import base.BaseBiz;
import event.AddressDeleteEvent;
import event.AddressRefreshEvent;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.biz.ManagerAddressBiz;
import ui.activity.mine.contract.ManagerAddressContract;
import ui.model.SortClass;

/**
 * Created by chenjian on 19/2/26
 */

public class ManagerAddressPresenter implements ManagerAddressContract.Presenter {

    ManagerAddressContract.View view;
    ManagerAddressBiz managerAddressBiz;
    List<FindAddressBean.DataBean.ItemsBean> list = new ArrayList<>();


    @Inject
    public ManagerAddressPresenter(ManagerAddressContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        managerAddressBiz = (ManagerAddressBiz) biz;
    }

    @Override
    public void getAddressList(boolean isRefresh, String id) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        GetBankcardParamBean g = new GetBankcardParamBean();
        g.setCustomId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(g));
        managerAddressBiz.getAddressList(requestBody, new BaseBiz.Callback<FindAddressBean.DataBean>() {
            @Override
            public void onSuccess(FindAddressBean.DataBean info) {
                list.addAll(info.getItems());
                SortClass sort = new SortClass();
                Collections.sort(list,sort);
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(FindAddressBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void deleteAddress(String id, int position) {
        view.loading(true);
        AddressParamBean a = new AddressParamBean();
        a.setAddressId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(a));
        managerAddressBiz.deleteAddress(requestBody, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.showMsg("删除地址成功");
                list.remove(position);
                view.upDateUI(list);
                view.loading(false);
                EventBus.getDefault().post(new AddressDeleteEvent(id));
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void defaultAddress(String userId, String id, int position) {
        view.loading(true);
        SetDefaultAddressParamBean s = new SetDefaultAddressParamBean();
        s.setCustomId(userId);
        s.setAddressId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(s));
        managerAddressBiz.setDefaultAddress(requestBody, new BaseBiz.Callback<SetDefaultAddressBean.DataBean>() {
            @Override
            public void onSuccess(SetDefaultAddressBean.DataBean info) {
                if (info.getItems().equals("设置成功")) {
                    view.showMsg("设置默认地址成功");
                    getAddressList(true, userId);
                } else {
                    view.showMsg(info.getItems());
                }
                view.loading(false);
                EventBus.getDefault().post(new AddressRefreshEvent());
            }

            @Override
            public void onFailure(SetDefaultAddressBean.DataBean info) {
                view.loading(false);
            }
        });
    }


}
